package com.cskaoyan;

import com.mysql.jdbc.Driver;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 创建日期: 2022/03/21 17:43
 *
 * @author ciggar
 * 查询
 */
public class QueryJDBC {


    static String url = "jdbc:mysql://localhost:3306/38th_1?useSSL=false&characterEncoding=utf8";
    static String username = "root";
    static String password = "123456";

    public static void main(String[] args) throws SQLException {

        // 1. 注册驱动
        DriverManager.registerDriver(new Driver());

        // 2. 建立连接
        Connection connection = DriverManager.getConnection(url,username,password);

        // 3. 获取statement对象
        Statement statement = connection.createStatement();

        // 4. 执行SQL语句
        ResultSet resultSet = statement.executeQuery("select * from city");

        // 5. 解析结果集
//        boolean ret = resultSet.next();
//
//        String name = resultSet.getString("name");
//
//        int id = resultSet.getInt("id");
//
//        System.out.println("name:"+ name + ",id:" + id);

        while (resultSet.next()) {

            String name = resultSet.getString("name");
            int id = resultSet.getInt("id");
            int pId = resultSet.getInt("p_id");
            System.out.println("name:"+ name + ",id:" + id + ",pId:" + pId);
        }

//        resultSet.afterLast();
//        resultSet.previous();
//
//        resultSet.beforeFirst();


        // 6. 释放资源
        try{
            resultSet.close();
        }finally {
            try{
                statement.close();
            }finally {
                connection.close();
            }
        }





    }


}
